package com.cqeec.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.cqeec.dao.StudentDao; 
import com.cqeec.domain.Student;

/**
 * 对学生的操作
 * 
 * @author 零起点
 *
 */
public class StudentDaoImp implements StudentDao {

	private PreparedStatement prst = null;

	/**
	 * studentNum 学生学号
	 * 
	 * @throws SQLException
	 */
	@Override
	public Student findStudentByID(Connection conn, String studentNum) {
		Student student = new Student();

		String sql = "SELECT * from student where studentNum = ?";

		try {
			prst = conn.prepareStatement(sql);
			prst.setString(1, studentNum);
			ResultSet rs = prst.executeQuery();
			if (rs.next()) {
				student.setStudentName(rs.getString("studentName").trim());
				student.setStudentNum(rs.getString("studentNum").trim());
				student.setStudentPwd(rs.getString("studentPwd").trim());
				student.setStudentMajor(rs.getString("studentMajor").trim());

				return student;
			}

		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "查询失败请联系管理员", "数据错误", JOptionPane.ERROR_MESSAGE);
			return null;
		}
		return student ;
	}

	/**
	 * 添加学生
	 */
	@Override
	public int addStudentByID(Connection com, Student student) {
		String sql = "INSERT INTO student VALUES(?,?,?,?,null)";// 学生课程代码 为空

		try {
			prst = com.prepareStatement(sql);
			prst.setString(1, student.getStudentName());
			prst.setString(2, student.getStudentNum());
			prst.setString(3, student.getStudentPwd());
			prst.setString(4, student.getStudentMajor());
			int rs = prst.executeUpdate();
			System.out.println(rs);
			if (rs != 0) {
				System.out.println(rs);
				return rs;
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "添加失败请联系管理员", "数据错误", JOptionPane.ERROR_MESSAGE);

		}
		return 0;

	}

	/**
	 * 查询学生考勤
	 */
	@Override
	public ResultSet getallCallRollImpl(Connection com) {
		ResultSet rs = null;
		//CallRoll callRoll = new CallRoll();
		String sql = "select * from call_roll";
		try {
			prst = com.prepareStatement(sql);
			rs = prst.executeQuery();
			// if(rs.next()) {
			// callRoll.setStudentName(rs.getString("studentName"));
			// callRoll.setCourseName(rs.getString("courseName"));
			// callRoll.setCourseAbsent(rs.getString("courseAbsent"));
			// return callRoll;
			// }
			return rs;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return rs;
	}

	/***
	 * = 按照学生班级代码 查找学生信息
	 * 
	 * @throws SQLException
	 */
	@Override
	public List<Student> findStudentByCourseNum(Connection conn, String classes) throws Exception {
		List<Student> studentList = new ArrayList<Student>();
		String sql = "select * from student where classes = ?";

		PreparedStatement pstm = conn.prepareStatement(sql);
		pstm.setString(1, classes);
		ResultSet rs = pstm.executeQuery();

		while (rs.next()) {
			Student student = new Student();
			student.setStudentName(rs.getString("studentName").trim());
			student.setStudentNum(rs.getString("studentNum").trim());
			student.setStudentPwd(rs.getString("studentPwd").trim());
			student.setStudentMajor(rs.getString("studentMajor").trim());
			student.setClasses(rs.getString("classes").trim());
			studentList.add(student);
		}

		return studentList;
	}

	@Override
	public void upateStudentByStudentName(Connection connection, Student student) throws SQLException {
		// TODO Auto-generated method stub
		String sql = "UPDATE student SET studentPwd = ? WHERE studentNum = ?";
		PreparedStatement psmt = connection.prepareStatement(sql);
		psmt.setString(1, student.getStudentPwd());
		psmt.setString(2, student.getStudentNum());
		psmt.executeUpdate();
	}

}
